#!/usr/bin/python

import datetime
import jspickle
import tt

class simpleClass :
    def __init__(self) :
        self.testing = 7

class getStateClass :
    def __init__(self) :
        pass

    def __getstate__(self) :
        print self.__class__ , "::__getstate__"
        return {'yes': 'hi'}

class getStateClass2 :
    def __init__(self) :
        pass

    def __getstate__(self) :
        #print self.__class__ , "::__getstate__"
        return 7

class getNewClass :
    def __init__(self, t = None) :
        pass

    def __getnewargs__(self) :
        #print self.__class__ , "::__getnewargs__"
        return {'yes': 'hi', 'dt' : datetime.datetime.now() }

class getNewClass2 :
    def __init__(self, d = {}, **kw) :
        pass

    def __getnewargs__(self) :
        #print self.__class__ , "::__getnewargs__"
        return {'yes': 'hi'}

    def __getstate__(self) :
        #print self.__class__ , "::__getstate__"
        return {'yes': 'hi'}

class getEncoder :
    def __init__(self, d = {}, **kw) :
        pass
    
def datetime_encoder(obj) :
    if datetime and isinstance(obj, datetime.datetime) :
        return obj.strftime("%Y%m%dT%H:%M:%S")
    if datetime and isinstance(obj, datetime.date) :
        return obj.strftime("%Y%m%dT%H:%M:%S")
    if datetime and isinstance(obj, datetime.time) :
        today = datetime.datetime.now().strftime("%Y%m%d")
        return obj.strftime(today+"T%H:%M:%S")
    return ""

def datetime_decoder(str) :
    dt = None
    try :
        return datetime.datetime.strptime("%Y%m%dT%H:%M:%S", str)
    except :
        pass
    try :
        return datetime.datetime.strptime("%H:%M:%S", str)
    except :
        pass
    try :
        return datetime.datetime.strptime("%Y%m%d", str)
    except :
        pass

jspickle.encoder["getEncoder"]        = lambda obj : "TEST"
jspickle.encoder["datetime.datetime"] = datetime_encoder
jspickle.decoder["datetime.datetime"] = datetime_decoder

#print jspickle.encode(getEncoder())
#print jspickle.encode(datetime.datetime.now())

if False :
    v = getNewClass2();
    print str(v.__class__)
    print str(v.__class__.__name__)
    print str(v.__class__.__dict__)

if True :
    print jspickle.encode(simpleClass())
    print jspickle.encode(getStateClass())
    print jspickle.encode(getStateClass2())
    print jspickle.encode(getNewClass())
    print jspickle.encode(getNewClass2())
    print jspickle.encode(tt.simpleImportClass())
    print jspickle.encode(tt.simpleImportSubClass())
    print jspickle.encode(datetime.datetime.now())

if False :
    strs = [
        '{"testing": 7, "__class__": "simpleClass"}',
        '{"yes": "hi", "__class__": "getStateClass"}',
        '{"__class__": "getStateClass2", "value": 7}',
        '{"__class__": "getNewClass", "__new__": [8]}',
        '{"__class__": "getNewClass", "__new__": {"yes": "hi"}}',
        '{"yes": "hi", "__class__": "getNewClass2", "__new__": [{"yes": "hi"}]}',
    ]

    for s in strs :
        v = jspickle.decode(s);
        print type(v)
        print v
